const playMode = {
  sequence: 0, // 顺序播放
  loop: 1, // 单曲循环
  random: 2// 随机播放
}
let mode = 0

// 根据播放模式调用函数
const getPlayList = function (mode, sequencePlayList) {
  // 播放模式为：顺序时
  if (mode === playMode.sequence) {
    return [...sequencePlayList]
    // 播放模式为：循环时
  } else if (mode === playMode.loop) {
    return [...sequencePlayList]
    // 播放模式为：随机时
  } else if (mode === playMode.random) {
    const newArr = [...sequencePlayList]
    // 将数组随机打乱
    for (let i = 0; i < newArr.length; i++) {
      const item = newArr[i]
      // 生成随机数
      const randomIndex = Math.floor(Math.random() * (i + 1))
      newArr[i] = newArr[randomIndex]
      newArr[randomIndex] = item
    }
    return newArr
  }
}
// 仓库的音乐播放器相关的状态
export default {
  namespaced: true,
  state: {
    // 是否要全屏播放
    fullScreen: false,
    // 当前音乐是否在播放
    playing: false,
    // 播放歌曲列表
    playList: [],
    // 顺序歌曲列表,原始的歌单列表
    sequencePlayList: [],
    // 播放模式
    mode: playMode.sequence,
    // 当前播放歌曲在歌曲列表中的下标
    currentIndex: -1
  },
  getters: {
    // 当前歌曲信息
    currentSong (state) {
      if (state.currentIndex >= 0 && state.currentIndex < state.sequencePlayList.length) {
        // 选中了歌曲要播放
        return state.playList[state.currentIndex]
      } else {
        // 没有选中歌曲要播放
        return {}
      }
    }
  },
  mutations: {
    // 歌单中点击歌曲后的处理事件
    selectSongByIndex (state, payload) {
      // 设置歌曲的下标
      state.currentIndex = payload.index
      // 歌单列表的原始数据需要一直保留，不能被其他事件影响到歌单顺序。
      state.sequencePlayList = [...payload.list]
      // 重新计算播放列表
      state.playList = getPlayList(state.mode, state.sequencePlayList)
      // 设置当前歌曲信息
      state.currentSong = state.sequencePlayList[state.currentIndex]
      // 找到歌曲所在播放歌曲的下标
      state.currentIndex = state.playList.findIndex(item =>
        item.id === state.currentSong.id
      )
      // 播放音乐
      state.playing = true
    },
    // 修改播放状态
    setPlaying (state, payload) {
      state.playing = payload.value
    },
    // 控制是否全屏播放
    setFullScreen (state, payload) {
      state.fullScreen = payload.value
    },
    // 点击切换播放模式
    changePlayModeAction (state) {
      mode += 1
      state.mode = mode % 3
      state.playList = getPlayList(state.mode, state.sequencePlayList)
      // 修改当前歌曲的index

      state.currentIndex = state.playList.findIndex(item =>
        item.id === state.currentSong.id
      )
      // 修改当前歌曲的信息
      state.currentSong = state.playList[state.currentIndex]
    },
    // 切换上一首或下一首
    switchPlayMusic (state, payload) {
      // 判断点击上一首或下一首
      if (payload.switchMUsic === 'prev') {
        // 切换下一首歌曲
        if (state.currentIndex > 0) {
          state.currentIndex--
        } else {
          state.currentIndex = state.playList.length
        }
      } else if (payload.switchMUsic === 'next') {
        // 切换下一首歌曲
        if (state.currentIndex < state.playList.length) {
          state.currentIndex++
        } else {
          state.currentIndex = 0
        }
      }
      // 更新当前播放的歌曲
      state.currentSong = state.playList[state.currentIndex]
      state.playing = true
    }

  },
  actions: {

  }
}
